package com.chengyu.core.service.pay.impl;

import com.chengyu.core.domain.CommonConstant;
import com.chengyu.core.domain.enums.AccountEnums;
import com.chengyu.core.domain.enums.OrderEnums;
import com.chengyu.core.domain.enums.PayEnum;
import com.chengyu.core.domain.form.PayBaseForm;
import com.chengyu.core.domain.form.RefundBaseForm;
import com.chengyu.core.exception.ServiceException;
import com.chengyu.core.model.TyOrder;
import com.chengyu.core.model.TyOrderRefund;
import com.chengyu.core.model.CommonPayOrder;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.service.member.MemberService;
import com.chengyu.core.service.pay.PayService;
import com.chengyu.core.service.pay.TyPayService;
import com.chengyu.core.service.pay.manager.PayManager;
import com.chengyu.core.service.ty.TyOrderRefundService;
import com.chengyu.core.service.ty.TyOrderService;
import com.chengyu.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TyPayServiceImpl implements TyPayService {

    @Autowired
    private PayManager payManager;
    @Autowired
    private TyOrderService tyOrderService;
    @Autowired
    private TyOrderRefundService orderRefundService;
    @Autowired
    private MemberService memberService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public String orderPay(UmsMember member, String payOrderNo, PayBaseForm baseForm) throws Exception {

        String substr = payOrderNo.substring(0, 3);
        System.out.println("substr:"+substr);
        if(!substr.equals("ty_")){
            throw new ServiceException("数据错误！");
        }


//        CommonPayOrder payOrder = tyOrderService.getPayOrder(payOrderNo);
//        System.out.println("payOrder:"+payOrder.toString());
//
//        payOrderNo = payOrderNo.substring(3, payOrderNo.length());
//        System.out.println("payOrderNo:"+payOrderNo);
//        System.out.println("tyOrdertyOrdertyOrdertyOrdertyOrder");
//
//
//
//        if (payOrder == null || payOrder.getStatus() != 0) {
//            throw new ServiceException("status.error");
//        }



        CommonPayOrder payOrder = tyOrderService.getPayOrder(payOrderNo);
        if (payOrder == null || payOrder.getStatus() != CommonConstant.WAIT_INT) {
            throw new ServiceException("status.error");
        }
        tyOrderService.updateOrderPaymethod(payOrderNo, baseForm.getPayMethod(), baseForm.getApplicationType());
        baseForm.setBody("订单支付-" + payOrderNo);
        if (PayEnum.PayMethod.WX_PAY.getValue().equals(baseForm.getPayMethod())) {
            baseForm.setPayNotifyUrl(PayService.TY_WXPAY_NOTIFY_URL_ORDER);
        } else if (PayEnum.PayMethod.ALI_PAY.getValue().equals(baseForm.getPayMethod())) {
            baseForm.setPayNotifyUrl(PayService.TY_ALIPAY_NOTIFY_URL_ORDER);
        } else if (PayEnum.PayMethod.BALANCE.getValue().equals(baseForm.getPayMethod())) {
            baseForm.setMemberAccountTypes(AccountEnums.MemberAccountTypes.ACCOUNT_TRADE_OUT);
            baseForm.setBody("购买商品");
        }
        String message = payManager.getPayFactory(baseForm.getPayMethod()).pay(member, payOrderNo, payOrder.getAmount(), baseForm);
        if ("noneedpay".equals(message)) {
            tyOrderService.paySus(payOrderNo);
        }
        return message;
    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void refund(String refundNo) throws ServiceException {
        TyOrderRefund refund = orderRefundService.getOrderRefundByRefundNo(refundNo);
        //查询出原订单
        TyOrder order = tyOrderService.getOrderById(refund.getOrderId());
        CommonPayOrder payOrder = tyOrderService.getPayOrder(order.getPayOrderNo());

        RefundBaseForm refundBaseForm = new RefundBaseForm();
        refundBaseForm.setOrderNo(order.getPayOrderNo());
        refundBaseForm.setRefundNo(refund.getRefundNo());
        refundBaseForm.setOrderAmount(payOrder.getAmount());
        refundBaseForm.setRefundAmount(refund.getRefundAmount());
        if (PayEnum.PayMethod.BALANCE.getValue().equals(payOrder.getPayMethod())) {
            refundBaseForm.setMember(memberService.getMemberById(payOrder.getMemberId()));
        }
        refundBaseForm.setPayChannel(payOrder.getPayChannel());
        //仅微信需要回调
        refundBaseForm.setRefundNotifyUrl(PayService.TY_WXPAY_NOTIFY_URL_REFUND);
        try {
            String message = payManager.getPayFactory(refund.getPayMethod()).refund(refundBaseForm);
            if (StringUtils.isNotBlank(message) && CommonConstant.SUCCESS.equals(message)) {
                orderRefundService.refundSusCallback(refund);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void cancelAndRefundOrder(String orderNo) throws ServiceException {
        TyOrder order = tyOrderService.getOrderByOrderNo(orderNo);
        if (order.getStatus() != OrderEnums.OrderStatus.WAIT_DELIVERY.getValue()) {
            throw new ServiceException("status.error");
        }

        RefundBaseForm refundBaseForm = new RefundBaseForm();
        refundBaseForm.setOrderNo(order.getPayOrderNo());
        refundBaseForm.setRefundNo(order.getOrderNo());
        refundBaseForm.setOrderAmount(order.getPayPrice());
        refundBaseForm.setRefundAmount(order.getPayPrice());
        if (PayEnum.PayMethod.BALANCE.getValue().equals(order.getPayMethod())) {
            refundBaseForm.setMember(memberService.getMemberById(order.getMemberId()));
        }
        refundBaseForm.setPayChannel(order.getPayChannel());
        //仅微信需要回调
        refundBaseForm.setRefundNotifyUrl(PayService.TY_WXPAY_NOTIFY_URL_CANCEL_ORDER);
        try {
            String message = payManager.getPayFactory(order.getPayMethod()).refund(refundBaseForm);
            if (StringUtils.isNotBlank(message) && CommonConstant.SUCCESS.equals(message)) {
                tyOrderService.cancelAndRefundOrderSus(order);
            }
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }


}
